﻿using Zhao.IServer.Log;
using Zhao.IServer.Management_Arena;
using Zhao.Models.DTO;
using Zhao.Models.Entity;
using Zhao.Repository.Base;
using Zhao.Repository.UnitOfWork;

namespace Zhao.Service.Management_Arena
{
    public class VenuesImageService : BaseService<VenuesImage>, IVenuesImageService
    {
        private readonly ILogsServer _logs;
        public VenuesImageService(IBaseRepository<VenuesImage> baseDal, IUnitOfWork unitOfWork, ILogsServer logs) : base(baseDal, unitOfWork)
        {
            _logs=logs;
        }



        public async Task<bool> UpdateImage(Guid venunId, List<VenuesImage> venunImages)
        {
            try
            {
                // 开始事务
                await _Client.Ado.BeginTranAsync();

                // 先删除原有的图片
                await _Client.Deleteable<VenuesImage>(x => x.VenueId == venunId).ExecuteCommandAsync();

                // 然后批量添加新图片
                await _Client.Insertable(venunImages).ExecuteCommandAsync();

                // 提交事务
                await _Client.Ado.CommitTranAsync();

                await _logs.Info($"球馆更改详情图片成功,球馆Id{venunId}");

                return true;
            }
            catch (Exception ex)
            {
                await _logs.Error($"球馆更改详情图片成失败,球馆Id{venunId},原因：{ex.Message}");
                // 回滚事务
                await _Client.Ado.RollbackTranAsync();
                // 通常应该记录或处理异常
                // 例如：_logger.LogError(ex, "更新图片失败");
                return false;
            }
        }
    }
}
